package com.openbravo.beans;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

public class JCalendarPanel extends JPanel
{
  private static LocaleResources m_resources;
  private static GregorianCalendar m_CalendarHelper = new GregorianCalendar();
  private Date m_date;
  private JButtonDate[] m_ListDates;
  private JLabel[] m_jDays;
  private JButtonDate m_jCurrent;
  private JButtonDate m_jBtnMonthInc;
  private JButtonDate m_jBtnMonthDec;
  private JButtonDate m_jBtnYearInc;
  private JButtonDate m_jBtnYearDec;
  private JButtonDate m_jBtnToday;
  private DateFormat fmtMonthYear = new SimpleDateFormat("MMMMM yyyy");
  private JPanel jPanel1;
  private JPanel jPanel2;
  private JPanel jPanel3;
  private JPanel m_jActions;
  private JPanel m_jDates;
  private JLabel m_jLblMonth;
  private JPanel m_jMonth;
  private JPanel m_jWeekDays;

  public JCalendarPanel()
  {
    this(new Date());
  }

  public JCalendarPanel(Date paramDate)
  {
    if (m_resources == null)
    {
      m_resources = new LocaleResources();
      m_resources.addBundleName("beans_messages");
    }
    initComponents();
    initComponents2();
    this.m_date = paramDate;
    renderMonth();
    renderDay();
  }

  public void setDate(Date paramDate)
  {
    Date localDate = this.m_date;
    this.m_date = paramDate;
    renderMonth();
    renderDay();
    firePropertyChange("Date", localDate, paramDate);
  }

  public Date getDate()
  {
    return this.m_date;
  }

  public void setEnabled(boolean paramBoolean)
  {
    super.setEnabled(paramBoolean);
    renderMonth();
    renderDay();
  }

  private void renderMonth()
  {
    for (int i = 0; i < 7; i++)
      this.m_jDays[i].setEnabled(isEnabled());
    JButtonDate localJButtonDate;
    for (int i = 0; i < 42; i++)
    {
      localJButtonDate = this.m_ListDates[i];
      localJButtonDate.DateInf = null;
      localJButtonDate.setEnabled(false);
      localJButtonDate.setText(null);
      localJButtonDate.setForeground((Color)UIManager.getDefaults().get("TextPane.foreground"));
      localJButtonDate.setBackground((Color)UIManager.getDefaults().get("TextPane.background"));
      localJButtonDate.setBorder(null);
    }
    if (this.m_date == null)
    {
      this.m_jLblMonth.setEnabled(isEnabled());
      this.m_jLblMonth.setText(null);
    }
    else
    {
      m_CalendarHelper.setTime(this.m_date);
      this.m_jLblMonth.setEnabled(isEnabled());
      this.m_jLblMonth.setText(this.fmtMonthYear.format(m_CalendarHelper.getTime()));
     int  i = m_CalendarHelper.get(2);
      m_CalendarHelper.set(5, 1);
      while (m_CalendarHelper.get(2) == i)
      {
        localJButtonDate = getLabelByDate(m_CalendarHelper.getTime());
        localJButtonDate.DateInf = m_CalendarHelper.getTime();
        localJButtonDate.setEnabled(isEnabled());
        localJButtonDate.setText(String.valueOf(m_CalendarHelper.get(5)));
        m_CalendarHelper.add(5, 1);
      }
    }
    this.m_jCurrent = null;
  }

  private void renderDay()
  {
    this.m_jBtnToday.setEnabled(isEnabled());
    if (this.m_date == null)
    {
      this.m_jBtnMonthDec.setEnabled(false);
      this.m_jBtnMonthInc.setEnabled(isEnabled());
      this.m_jBtnYearDec.setEnabled(isEnabled());
      this.m_jBtnYearInc.setEnabled(isEnabled());
    }
    else
    {
      m_CalendarHelper.setTime(this.m_date);
      m_CalendarHelper.add(2, -1);
      this.m_jBtnMonthDec.DateInf = m_CalendarHelper.getTime();
      this.m_jBtnMonthDec.setEnabled(isEnabled());
      m_CalendarHelper.add(2, 2);
      this.m_jBtnMonthInc.DateInf = m_CalendarHelper.getTime();
      this.m_jBtnMonthInc.setEnabled(isEnabled());
      m_CalendarHelper.setTime(this.m_date);
      m_CalendarHelper.add(1, -1);
      this.m_jBtnYearDec.DateInf = m_CalendarHelper.getTime();
      this.m_jBtnYearDec.setEnabled(isEnabled());
      m_CalendarHelper.add(1, 2);
      this.m_jBtnYearInc.DateInf = m_CalendarHelper.getTime();
      this.m_jBtnYearInc.setEnabled(isEnabled());
      if (this.m_jCurrent != null)
      {
        this.m_jCurrent.setForeground((Color)UIManager.getDefaults().get("TextPane.foreground"));
        this.m_jCurrent.setBackground((Color)UIManager.getDefaults().get("TextPane.background"));
        this.m_jCurrent.setBorder(null);
      }
      JButtonDate localJButtonDate = getLabelByDate(this.m_date);
      localJButtonDate.setBackground((Color)UIManager.getDefaults().get("TextPane.selectionBackground"));
      localJButtonDate.setForeground((Color)UIManager.getDefaults().get("TextPane.selectionForeground"));
      localJButtonDate.setBorder(new LineBorder((Color)UIManager.getDefaults().get("TitledBorder.titleColor")));
      this.m_jCurrent = localJButtonDate;
    }
  }

  private JButtonDate getLabelByDate(Date paramDate)
  {
    GregorianCalendar localGregorianCalendar = new GregorianCalendar();
    localGregorianCalendar.setTime(paramDate);
    int i = localGregorianCalendar.get(5);
    localGregorianCalendar.set(5, 1);
    int j = localGregorianCalendar.get(7) - localGregorianCalendar.getFirstDayOfWeek();
    if (j < 0)
      j += 7;
    return this.m_ListDates[(j + i - 1)];
  }

  private void initComponents2()
  {
    DateClick localDateClick = new DateClick( );
    this.m_jBtnYearDec = new JButtonDate(new ImageIcon(getClass().getResource("/com/openbravo/images/2uparrow.png")), localDateClick);
    this.m_jBtnMonthDec = new JButtonDate(new ImageIcon(getClass().getResource("/com/openbravo/images/1uparrow.png")), localDateClick);
    this.m_jBtnToday = new JButtonDate(m_resources.getString("Button.Today"), localDateClick);
    this.m_jBtnMonthInc = new JButtonDate(new ImageIcon(getClass().getResource("/com/openbravo/images/1downarrow.png")), localDateClick);
    this.m_jBtnYearInc = new JButtonDate(new ImageIcon(getClass().getResource("/com/openbravo/images/2downarrow.png")), localDateClick);
    this.m_jBtnToday.DateInf = new Date();
    this.m_jActions.add(this.m_jBtnYearDec);
    this.m_jActions.add(this.m_jBtnMonthDec);
    this.m_jActions.add(this.m_jBtnToday);
    this.m_jActions.add(this.m_jBtnMonthInc);
    this.m_jActions.add(this.m_jBtnYearInc);
    this.m_ListDates = new JButtonDate[42];
    for (int i = 0; i < 42; i++)
    {
    	JButtonDate   localObject = new JButtonDate(localDateClick);//ati add defi
      ((JButtonDate)localObject).setHorizontalAlignment(0);
      ((JButtonDate)localObject).setText(null);
      ((JButtonDate)localObject).setOpaque(true);
      ((JButtonDate)localObject).setForeground((Color)UIManager.getDefaults().get("TextPane.foreground"));
      ((JButtonDate)localObject).setBackground((Color)UIManager.getDefaults().get("TextPane.background"));
      ((JButtonDate)localObject).setBorder(null);
      this.m_ListDates[i] = localObject;
      this.m_jDates.add((Component)localObject);
    }
    this.m_jDays = new JLabel[7];
    for (int i = 0; i < 7; i++)
    {
    	JLabel   localObject = new JLabel();//ati add defi
      ((JLabel)localObject).setHorizontalAlignment(0);
      this.m_jDays[i] = localObject;
      this.m_jWeekDays.add((Component)localObject);
    }
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("E");
    Object localObject = new GregorianCalendar();
    for (int k = 0; k < 7; k++)
    {
      ((Calendar)localObject).add(5, 1);
      int j = ((Calendar)localObject).get(7) - ((Calendar)localObject).getFirstDayOfWeek();
      if (j < 0)
        j += 7;
      this.m_jDays[j].setText(localSimpleDateFormat.format(((Calendar)localObject).getTime()));
    }
  }

  private void initComponents()
  {
    this.jPanel1 = new JPanel();
    this.m_jMonth = new JPanel();
    this.m_jWeekDays = new JPanel();
    this.m_jDates = new JPanel();
    this.jPanel2 = new JPanel();
    this.m_jLblMonth = new JLabel();
    this.jPanel3 = new JPanel();
    this.m_jActions = new JPanel();
    setLayout(new BorderLayout());
    this.jPanel1.setBorder(BorderFactory.createEtchedBorder());
    this.jPanel1.setLayout(new BorderLayout());
    this.m_jMonth.setLayout(new BorderLayout());
    this.m_jWeekDays.setLayout(new GridLayout(1, 7));
    this.m_jMonth.add(this.m_jWeekDays, "North");
    this.m_jDates.setBackground(UIManager.getDefaults().getColor("TextPane.background"));
    this.m_jDates.setLayout(new GridLayout(6, 7));
    this.m_jMonth.add(this.m_jDates, "Center");
    this.jPanel1.add(this.m_jMonth, "Center");
    this.m_jLblMonth.setFont(new Font("Dialog", 1, 14));
    this.jPanel2.add(this.m_jLblMonth);
    this.jPanel1.add(this.jPanel2, "North");
    add(this.jPanel1, "Center");
    this.jPanel3.setLayout(new BorderLayout());
    this.m_jActions.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    this.m_jActions.setLayout(new GridLayout(0, 1, 0, 5));
    this.jPanel3.add(this.m_jActions, "North");
    add(this.jPanel3, "After");
  }

  private static class JButtonDate extends JButton
  {
    public Date DateInf;

    public JButtonDate(ActionListener paramActionListener)
    {
      initComponent();
      addActionListener(paramActionListener);
    }

    public JButtonDate(String paramString, ActionListener paramActionListener)
    {
      super();
      initComponent();
      addActionListener(paramActionListener);
    }

    public JButtonDate(Icon paramIcon, ActionListener paramActionListener)
    {
      super();
      initComponent();
      addActionListener(paramActionListener);
    }

    private void initComponent()
    {
      this.DateInf = null;
      setRequestFocusEnabled(false);
      setFocusPainted(false);
      setFocusable(false);
    }
  }

  private class DateClick
    implements ActionListener
  {
    private DateClick()
    {
    }

    public void actionPerformed(ActionEvent paramActionEvent)
    {
      JCalendarPanel.JButtonDate localJButtonDate = (JCalendarPanel.JButtonDate)paramActionEvent.getSource();
      if (localJButtonDate.DateInf != null)
        JCalendarPanel.this.setDate(localJButtonDate.DateInf);
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.beans.JCalendarPanel
 * JD-Core Version:    0.6.0
 */